app.controller('pv11', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //pv任务
    $scope.pv3_0 = function () {
        $state.go(pv3_0.stateName);
    }


    //获得数据
    $scope.submit = function () {
        //TODO  此处的签名需存储
        if(!$scope.caseId){
            return;
        }

        if ($("#signature").jSignature("getData", "native").length == 0) {
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
            return false;
        }

        //获得签名
        importSign('signature', 'signature', function (result) {
          var json = JSON.stringify(result)
          var jsonData = JSON.parse(json).obj;
          for (var i = 0; i < jsonData.length; i++) {
              url = jsonData[i].url;
          }
            if(2==$scope.signType){
                $scope.feSignUrl = url;
                //存签名
                localStorage.setItem("pv-feSignUrl",url);
            }else if(1==$scope.signType){
                $scope.customerSignUrl = url;
                //存签名
                localStorage.setItem("pv-customerSignUrl",url);
            }


            $scope.customerEmail = $(".customer-email").val();
            ///提交签名
            var postData = {
                "ids": [$scope.caseId],
                "feSignUrl": $scope.feSignUrl,
                "customerSignUrl": $scope.customerSignUrl,
                "customerEmail":$scope.customerEmail
            };
            postHttp($http, 'pvCase/commitSign', postData, function (res) {
                if (res.code === 0 && res.msg == "success") {
                    $scope.pv3_0();
                    $scope.$applyAsync();
                } else {
                    $rootScope.tipPopups.show(($scope.tipMessage = res.msg));
                }
            });

         });


    }

    //初始化
    function init(){
        $scope.feSignUrl = "";
        $scope.customerSignUrl = "";

        //签名类型
        $scope.signType = pv11.params.type;
        if(!pv11.params.type){
            $scope.signType = localStorage.getItem("pv-type");
        }
        //案例id
        $scope.caseId = pv11.params.caseId;
        if(!pv11.params.caseId){
            $scope.caseId = localStorage.getItem("pv-caseId");
        }

        if(1==$scope.signType){
            $scope.signTypeName = "客户";
        }else{
            $scope.signTypeName = "工程师";
        }
    }

    init();


    //初始化画布
    $(document).ready(function(){
        var innnerheight = window.innerHeight;
        var deHeight = 200;
        if(1!=$scope.signType){
            deHeight = 250;
        }
        var height=(innnerheight - deHeight)+'px';
        document.getElementById("sign").style.height = height;

        var width=100+'%';
        $("#signature").jSignature({height:height,width:width,color:"#000",lineWidth:4});
    })
    //重置画布
    $scope.clearSig = function(){
        $("#signature").jSignature("clear");
    }
    //将画布内容显示到IMG
    $scope.importImg = function(){
        var sig=$("#signature");
        $("#img").attr("src","data:"+sig.jSignature('getData'));
        var sign = sig.jSignature("getData");
        if(1==$scope.signType){
            $scope.feSignUrl = sign;
            //存签名
            localStorage.setItem("pv-feSignUrl",sign);
        }else if(2==$scope.signType){
            $scope.customerSignUrl = sign;
            //存签名
            localStorage.setItem("pv-customerSignUrl",sign);
        }
    }

    reloadJs();

});